(function ($, window, document) {

	function ThePaging (el, opt) {
		this.dom = $(el);

		opt = opt || {};

		this.current = opt.current || 1;
		this.size = opt.size || 20;
		this.total = opt.total || 0; // 总条数
		this.pages = 0; // 总页数

		// 当前页改变回调
		if (opt.currentChange) {
			this.currentChange = opt.currentChange;
		}

		this._init();
	}

	ThePaging.prototype = {
		_init: function () {
			this._initState();
			this._initEvent();
		},
		_initState: function () {
			this.dom.addClass('_the-paging-box');
			this._initHtml(); // 初始化结构
		},
		_initEvent: function () {
			var _this = this;

			this.dom.on('click', '._btn-box .prev-btn', function () {
				var oThis = $(this);
				if (oThis.hasClass('disabled')) {
					return;
				}
				_this.setCurrent(_this.current-1);
			})
			this.dom.on('click', '._btn-box .next-btn', function () {
				var oThis = $(this);
				if (oThis.hasClass('disabled')) {
					return;
				}
				_this.setCurrent(_this.current+1);
			})
			this.dom.on('click', '._btn-box .num-btn', function () {
				var oThis = $(this);
				if (oThis.hasClass('active')) {
					return;
				}
				_this.setCurrent(oThis.html());
			})


		},
		// 初始化结构
		_initHtml: function () {
			var html = '<div class="_btn-box">' +
					'<span class="item btn prev-btn">&lt;</span>' +
					'<span class="item btn num-btn">1</span>' +
					'<span class="item btn next-btn">&gt;</span>' +
				'</div>' +
				'<div class="_text-box">当前<span class="current">1</span>页，每页<span class="size">' + this.size + '</span>条，共<span class="total">0</span>条</div>';
			this.dom.html(html);
			this._setDefault();
		},

		// 根据当前页设置默认
		_setDefault: function () {
			if (this.current == 1) {
				this.dom.find('._btn-box .prev-btn').addClass('disabled');
			} else if (this.current == this.total) {
				this.dom.find('._btn-box .next-btn').addClass('disabled');
			}
			var oNum = this.dom.find('._btn-box .num-btn');
			for (var i = 0; i < oNum.length; i++) {
				var oItem = $(oNum[i]);
				if (oItem.html() == this.current) {
					oItem.addClass('active');
					break;
				}
			}
		},

		// 设置总页数
		setTotal: function (total) {
			this.total = total;
			this.pages = Math.ceil(this.total / this.size);
			if (!(this.pages > 0)) {
				return;
			}
			var html = '<span class="item btn prev-btn">&lt;</span>';
			// 总数小于5
			if (this.pages <= 5) {
				var max = this.pages > 0 ? this.pages : 1;
				for (var i = 0; i < max; i++) {
					html += '<span class="item btn num-btn">' + (i+1) + '</span>';
				}
			} else {
				html += '<span class="item btn num-btn">1</span>';
				// 当前页大于3
				if (this.current > 3) {
					html += '<span class="item dot">···</span>';
				}
				// 根据当前页计算从几开始
				var start = this.current - 1;
				if (start <= 1) {
					start = 2;
				} else if (this.pages - start <= 2) {
					start = this.pages - 3;
				}
				for (var i = 0; i < 3; i++) {
					html += '<span class="item btn num-btn">' + (start + i) + '</span>';
				}
				// 当前页小于总数-2
				if (this.pages - 2 > this.current) {
					html += '<span class="item dot">···</span>';
				}
				html += '<span class="item btn num-btn">' + this.pages + '</span>';
			}
			html += '<span class="item btn next-btn">&gt;</span>';
			this.dom.find('._btn-box').html(html);

			// 设置当前页文本
			this.dom.find('._text-box .current').html(this.current);
			// 设置每页条数文本
			this.dom.find('._text-box .size').html(this.size);
			// 设置总条数文本
			this.dom.find('._text-box .total').html(this.total);

			// 设置默认状态
			this._setDefault();
		},
		// 设置当前页
		setCurrent: function (current) {
			this.current = current;
			this.setTotal(this.total);
			if (this.currentChange) {
				this.currentChange(this.current, this.size, this.total);
			}
		},

	}

window.ThePaging = ThePaging;

})($, window, document);